/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import { getList, getCount, getCourier, assignTo, cancelOrder } from "@/services/allot";

export default {
  namespace: "allot",

  state: {
    allotData: {},
    countData: null,
    courierData: null
  },

  effects: {
    * getList({ payload, force = false }, { put, call, select }) {
      const { allotData } = yield select(_ => _.allot);
      const { page = 1, target, searchc, searchd } = payload;
      const allotDataTarget = allotData[target];
      if (!force && allotDataTarget) {
        const { page: cPage, searchc: cSearchc, searchd: cSearchd } = allotDataTarget.body;
        if (page == cPage && searchc === cSearchc && searchd === cSearchd) return;
      }
      const response = yield call(getList, payload);
      if (!response) return;
      const { code, data } = response;
      if (code === 0 && data) {
        const { list } = data;
        data.body = {
          ...payload
        };
        yield put({
          type: "saveAllotData",
          payload: {
            [target]: list && list.length > 0 ? data : null
          }
        });
      }
    },
    * getCount({ payload }, { put, call }) {
      const response = yield call(getCount, payload);
      if (!response) return;
      const { code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            countData: {
              ...data
            }
          }
        });
      }
    },
    * getCourier({ payload }, { put, call }) {
      const response = yield call(getCourier, payload);
      if (!response) return;
      const { code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            courierData: {
              // ...data
              working:data.working||0,
              no_working:data.no_working||0,
              result:data.result||[],
              ...data
            }
          }
        });
      }
    },
    * assignTo({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(assignTo, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        let tips,
          resetData = { accepted: null };
        if (payload.type == 1) {
          tips = "已分配";
          resetData.published = null;
        } else {
          tips = "已改派";
        }
        message.success(tips);
        // 清空已取消列表，操作发生当前列表
        yield put({
          type: "saveAllotData",
          payload: {
            ...resetData
          }
        });
      } else {
        message.error(msg);
      }
      __dva_resolve(response);
    },
    * cancelOrder({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(cancelOrder, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success("已取消");
        // 清空已取消列表，操作发生当前列表
        yield put({
          type: "saveAllotData",
          payload: {
            [payload.cancel]: null,
            canceled: null
          }
        });
      } else {
        message.error(msg);
      }
      __dva_resolve(response);
    }
  },

  reducers: {
    saveAllotData(state, { payload }) {
      return {
        ...state,
        allotData: {
          ...state.allotData,
          ...payload
        }
      };
    },
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};
